package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private String ip;
    private int port;

    public String getIp(){
        return this.ip;
    }

    public void setIp(String ip){
        this.ip = ip;
    }

    public int getPort(){
        return this.port;
    }

    public void setPort(int port){
        this.port = port;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
    }

    @SuppressLint("SetTextI18n")
    private void initUI() {
        findViewById(R.id.button2).setOnClickListener(this);
        EditText editText2 = findViewById(R.id.editTextTextPostalAddress2);
        EditText editText3 = findViewById(R.id.editTextTextPostalAddress3);
        editText3.setText("1234");
        editText2.setText("192.168.4.2");
    }

    @Override
    public void onClick(View view) {
        EditText editText2 = findViewById(R.id.editTextTextPostalAddress2);
        EditText editText3 = findViewById(R.id.editTextTextPostalAddress3);
        setIp(String.valueOf(editText2.getText()));
        try{
            setPort(Integer.parseInt(String.valueOf(editText3.getText())));
        }
        catch (NumberFormatException e){
            Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
            return;
        }
        MainActivity.CustomCallableConnect cConnect = new MainActivity.CustomCallableConnect();
        FutureTask<TCPClient> futureTaskConnect = new FutureTask<>(cConnect);
        Thread threadConnect = new Thread(futureTaskConnect, "连接子线程");
        threadConnect.start(); //连接子线程执行
        try {
            futureTaskConnect.get();
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent();
        //跳转到第二个控制界面
        if(TCPClient.getInstance().isConnected()){
            Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
            intent.setClass(getApplicationContext(), ControlActivity.class);
            startActivity(intent);
        }
        else{
            Toast.makeText(getApplicationContext(), "连接失败", Toast.LENGTH_SHORT).show();
        }
    }

    class CustomCallableConnect implements Callable<TCPClient> {
        public TCPClient call() {
            TCPClient tcpClient = TCPClient.getInstance();
            tcpClient.setClient(getIp(),getPort());
            tcpClient.connect(); //连接TCPServer
            return tcpClient;
        }
    }

}


